package com.hdf.etdemo.friend.adapter;

import java.util.ArrayList;
import java.util.List;

import com.hdf.easytools.image.XCRoundImageView;
import com.hdf.etdemo.R;
import com.hdf.etdemo.friend.entity.FriendEntiy;
import com.hdf.etdemo.friend.utils.SysUtils;
import com.hdf.etdemo.friend.view.NewGridView;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class FriendListAdapter extends BaseAdapter {
	private LayoutInflater layoutInflater;
	private Activity context;
	private List<FriendEntiy> appLists;
	private FriendGridAdapter friendInfoImgsAdapter;
	private int wh;

	public FriendListAdapter(Activity cxt, List<FriendEntiy> data) {
		super();
		this.context = cxt;
		this.layoutInflater = LayoutInflater.from(context);
		this.wh = (SysUtils.getScreenWidth(context) - SysUtils.Dp2Px(context, 99)) / 3;
		this.appLists = data;
	}

	@Override
	public int getCount() {
		return appLists == null ? 0 : appLists.size();
	}

	@Override
	public Object getItem(int position) {
		return appLists == null ? null : appLists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return appLists == null ? null : position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (appLists.size() == 0) {
			return null;
		}
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.item_friend, null);
			holder.headphoto = (XCRoundImageView) convertView.findViewById(R.id.info_iv_header);// 头像
			holder.disName = (TextView) convertView.findViewById(R.id.info_tv_name);// 昵称
			holder.time = (TextView) convertView.findViewById(R.id.info_tv_time);// 时间
			holder.content = (TextView) convertView.findViewById(R.id.info_tv_content);// 发布内容
			holder.rl4 = (RelativeLayout) convertView.findViewById(R.id.rl4);// 图片布局
			holder.gv_images = (NewGridView) convertView.findViewById(R.id.gv_images);// 图片
			holder.headphoto.setType(XCRoundImageView.TYPE_ROUND);
			holder.headphoto.setRoundBorderRadius(20);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		final FriendEntiy entiy = (FriendEntiy) getItem(position);
		String name = null, time = null, content = null, headpath = null;
		List<String> imgList = null;
		if (entiy != null) {
			name = entiy.getFname();
			time = entiy.getFtime();
			content = entiy.getFcontent();
			headpath = entiy.getFicon();
			imgList = entiy.getFprice();
		}
		// 昵称
		if (name != null && !name.equals("")) {
			holder.disName.setText(name);
		}
		// 是否含有图片
		if (imgList != null && imgList.size() != 0) {
			holder.rl4.setVisibility(View.VISIBLE);
			initInfoImages(holder.gv_images,imgList);
		} else {
			holder.rl4.setVisibility(View.GONE);
		}
		// 发布时间
		if (time != null && !time.equals("")) {
			holder.time.setText(time);
		}
		// 内容
		if (content != null && !content.equals("")) {
			holder.content.setText(content);
			Linkify.addLinks(holder.content, Linkify.WEB_URLS);
		}
		// 头像
		if (headpath != null && !headpath.equals("")) {
			ImageLoader.getInstance().displayImage(headpath, holder.headphoto);
		} else {
			holder.headphoto.setImageResource(R.drawable.liuyifei);
		}
		holder.headphoto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Toast.makeText(context, "点击了头像", Toast.LENGTH_LONG).show();
			}
		});
		return convertView;
	}

	public void setAppList(ArrayList<FriendEntiy> inforL) {
		this.appLists = inforL;
	}

	public void addAppList(ArrayList<FriendEntiy> inforL) {
		this.appLists = inforL;
	}

	public static class ViewHolder {
		XCRoundImageView headphoto;
		TextView disName;
		TextView time;
		TextView content;
		NewGridView gv_images;
		RelativeLayout rl4;
	}

	public void initInfoImages(NewGridView newGridView, final List<String> imgList) {
		int w = 0;
		switch (imgList.size()) {
		case 1:
			w = wh;
			newGridView.setNumColumns(1);
			break;
		case 2:
		case 4:
			w = 2 * wh + SysUtils.Dp2Px(context, 2);
			newGridView.setNumColumns(2);
			break;
		case 3:
		case 5:
		case 6:
			w = wh * 3 + SysUtils.Dp2Px(context, 2) * 2;
			newGridView.setNumColumns(3);
			break;
		}
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(w, RelativeLayout.LayoutParams.WRAP_CONTENT);
		newGridView.setLayoutParams(lp);
		friendInfoImgsAdapter = new FriendGridAdapter(context, imgList);
		newGridView.setAdapter(friendInfoImgsAdapter);
		newGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Toast.makeText(context, "点击了第" + (arg2 + 1) + "张图片", Toast.LENGTH_LONG).show();
			}
		});
	}

}
